package org.singledog.form.builder.parser.freemarker.freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.singledog.form.builder.parser.freemarker.directions.BuilderIncludeDirectiveModel;
import org.singledog.form.builder.parser.freemarker.directions.DatagridBuilderTemplateDirectiveModel;
import org.singledog.form.builder.parser.freemarker.directions.FormBuilderTemplateDirectiveModel;
import org.singledog.form.builder.parser.freemarker.loader.ClasspathTemplateLoader;

import java.io.IOException;

public class FreeMarkerConfiguration {

    private static final Configuration configuration;
    private static boolean useCache = true;

    static {
        configuration = new Configuration();
        configuration.setTemplateLoader(new ClasspathTemplateLoader("templates"));
        configuration.setDefaultEncoding("UTF-8");

        configuration.setSharedVariable("formBuilder", new FormBuilderTemplateDirectiveModel());
        configuration.setSharedVariable("datagridBuilder", new DatagridBuilderTemplateDirectiveModel());
        configuration.setSharedVariable("includeBuilder", new BuilderIncludeDirectiveModel());
    }

    public static Template getTemplate(String name) throws IOException {
        return configuration.getTemplate(name);
    }

    public static void setUseCache(boolean useCache) {
        FreeMarkerConfiguration.useCache = useCache;
    }

    public static boolean isUseCache() {
        return useCache;
    }
}
